package com.benjaminwan.read.free.views

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.DrawableRes
import com.airbnb.epoxy.CallbackProp
import com.airbnb.epoxy.ModelProp
import com.airbnb.epoxy.ModelView
import com.airbnb.epoxy.TextProp
import com.benjaminwan.read.free.R
import com.mywork.swipemenulayout.SwipeMenuItem
import com.mywork.swipemenulayout.SwipeMenuLayout
import com.mywork.swipemenulayout.SwipeMenuView
import com.xuexiang.xui.widget.textview.autofit.AutoFitTextView

@ModelView(autoLayout = ModelView.Size.MATCH_WIDTH_WRAP_HEIGHT)
class ActionItemView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private val contentLayout by lazy { findViewById<FrameLayout>(R.id.contentLayout) }
    private val swipeMenuLayout by lazy { findViewById<SwipeMenuLayout>(R.id.swipeMenuLayout) }
    private val swipeDirectionIV by lazy { findViewById<ImageView>(R.id.swipeDirectionIV) }
    private val actionIndexTv by lazy { findViewById<AutoFitTextView>(R.id.actionIndexTv) }
    private val progressTv by lazy { findViewById<TextView>(R.id.progressTv) }
    private val actionTypeTv by lazy { findViewById<TextView>(R.id.actionTypeTv) }
    private val actionParamTv by lazy { findViewById<AutoFitTextView>(R.id.actionParamTv) }
    private val actionItemLayout by lazy { findViewById<LinearLayout>(R.id.actionItemLayout) }
    private val rightLayout by lazy { findViewById<SwipeMenuView>(R.id.rightLayout) }

    init {
        //View.inflate(context, R.layout.rv_action_item, this)
        View.inflate(context, R.layout.rv_common_swipe_menu_item, this)
        LayoutInflater.from(this.context)
            .inflate(R.layout.rv_action_item, this.contentLayout)
        swipeMenuLayout.onMenuRightOpenListener = {
            swipeDirectionIV.setImageResource(R.drawable.ic_swipe_right_black_24dp)
        }
        swipeMenuLayout.onMenuCloseListener = {
            swipeDirectionIV.setImageResource(R.drawable.ic_swipe_left_black_24dp)
        }
    }

    private var downX = 0
    private var downY = 0
    private var hasSwipeMenu = false

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if (!hasSwipeMenu) {
            return super.dispatchTouchEvent(ev)
        }
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                downX = ev.x.toInt()
                downY = ev.y.toInt()
                parent?.requestDisallowInterceptTouchEvent(true)
            }

            MotionEvent.ACTION_MOVE -> {
                val moveX = ev.x.toInt()
                val moveY = ev.y.toInt()
                val diffX = moveX - downX
                val diffY = moveY - downY
                if (Math.abs(diffX) > Math.abs(diffY)) { // 当前是横向滑动, 不让父元素拦截
                    parent?.requestDisallowInterceptTouchEvent(true)
                } else {
                    parent?.requestDisallowInterceptTouchEvent(false)
                }
            }

            else -> {
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    @TextProp
    fun setActionIndex(content: CharSequence) {
        actionIndexTv.text = content
    }

    @TextProp
    fun setActionProgress(content: CharSequence?) {
        if (content.isNullOrEmpty()) {
            progressTv.visibility = View.GONE
        } else {
            progressTv.visibility = View.VISIBLE
        }
        progressTv.text = content
    }

    @TextProp
    fun setActionType(content: CharSequence) {
        actionTypeTv.text = content
    }

    @TextProp
    fun setActionParam(content: CharSequence) {
        actionParamTv.text = content
    }

    @ModelProp
    fun setBackgroundRes(@DrawableRes id: Int) {
        actionItemLayout.setBackgroundResource(id)
    }

    @ModelProp
    fun setMarginLeft(margin: Int) {
        val layoutParams = contentLayout.layoutParams as MarginLayoutParams
        layoutParams.setMargins(margin * 10, 0, 0, 0)
        contentLayout.layoutParams = layoutParams
    }

    @CallbackProp
    fun onClickListener(listener: View.OnClickListener?) {
        actionItemLayout.setOnClickListener(listener)
    }

    @ModelProp
    @JvmOverloads
    fun setRightMenu(rightMenu: List<SwipeMenuItem>? = null) {
        if (rightMenu == null || rightMenu.isEmpty()) {
            hasSwipeMenu = false
            rightLayout.removeAllViews()
            swipeMenuLayout.isCanRightSwipe = false
            swipeDirectionIV.visibility = View.GONE
        } else {
            hasSwipeMenu = true
            rightLayout.createMenu(rightMenu)
            swipeMenuLayout.isCanRightSwipe = true
            swipeDirectionIV.visibility = View.VISIBLE
        }
    }

    @CallbackProp
    fun onRightMenuClickListener(listener: ((swipeLayout: SwipeMenuLayout, item: SwipeMenuItem) -> Unit)?) {
        rightLayout.setOnMenuItemClickListener { item ->
            listener?.invoke(swipeMenuLayout, item)
        }
    }

}